#include <16F877A.h>
#include<stdio.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
//#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)

#byte PORTB=6

#define CLK PIN_B0
#define  DTA PIN_B1

int8 volume;
unsigned int i;
void sendcommand(short command)
{
   PORTB &= ~( 1<<DTA );
 	PORTB |= (1 << CLK );
   delay_ms(5);   //tRP
 	delay_ms(300);     //tRESET

   PORTB &= ~( 0<<CLK );		// CLK low
 	delay_ms(2);

	for (i=1; i<=16; i++)
   {
      PORTB &= ~( 1<<DTA );		// Data low
     	if (command & 0x8000)
      {		// check MSB and if 1 put DTA high
         PORTB |= (1 << DTA );
      }
		else
          PORTB&= ~( 1<<DTA );

		delay_us(100);

		PORTB &= ~( 1<< CLK );
     	delay_us(100);

		PORTB |= (1 << CLK );
		command = command * 2;
		delay_us(100);
	}
	PORTB |= (1 << CLK );
	delay_ms(2);
}

void setVolume(void)
{
   if (volume > 7) volume = 7;
	sendcommand(0xfff0+volume);
}

void main()
{
   port_b_pullups(TRUE);
   output_b(0x00);
   output_c(0xff);

   delay_ms(2000);	// wait for start
	volume=6;
	setVolume();
	sendcommand(0);		// Play file 000.ad4
   delay_ms(2000);
  // sendcommand(1);

}
